package com.buptlogistics.creditinvestigation.activity;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;

import com.avos.avoscloud.AVClassName;
import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVOSCloud;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.SaveCallback;
import com.buptlogistics.creditinvestigation.R;
import com.buptlogistics.creditinvestigation.app.BaseFragmentActivity;
import com.buptlogistics.creditinvestigation.fragment.AccountFragment;
import com.buptlogistics.creditinvestigation.fragment.CircleFragment;
import com.buptlogistics.creditinvestigation.fragment.MainFragment;
import com.buptlogistics.creditinvestigation.fragment.MoreFragment;

import it.neokree.materialtabs.MaterialTab;
import it.neokree.materialtabs.MaterialTabHost;
import it.neokree.materialtabs.MaterialTabListener;

//                    _oo0oo_
//                   o8888888o
//                   88" . "88
//                   (| -_- |)
//                   0\  =  /0
//                 ___/`___'\___
//               .' \\|     |// '.
//              / \\|||  :  |||// \
//             / _||||| -:- |||||_ \
//            |   | \\\  _  /// |   |
//            | \_|  ''\___/''  |_/ |
//            \  .-\__  '_'  __/-.  /
//          ___'. .'  /--.--\  '. .'___
//        ."" '<  .___\_<|>_/___. '>' "".
//     | | :  `_ \`.;` \ _ / `;.`/ - ` : | |
//     \ \  `_.   \_ ___\ /___ _/   ._`  / /
//  ====`-.____` .__ \_______/ __. -` ___.`====
//                   `=-----='
//
//  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//        佛祖保佑           永无BUG
// ┏ ┓　　 ┏ ┓
//┏┛ ┻━━━━━┛  ┻┓
//┃　　　　　　 ┃
//┃　　　━　　　┃
//┃　┳┛　  ┗┳　┃
//┃　　　　　　 ┃
//┃　　　┻　　　┃
//┃　　　　　　 ┃
//┗━┓　　　┏━━━┛
//  ┃　　　┃   神兽保佑
//  ┃　　　┃   代码无BUG！
//  ┃　　　┗━━━━━━━━━┓
//  ┃　　　　　　　    ┣┓
//  ┃　　　　         ┏┛
//  ┗━┓ ┓ ┏━━━┳ ┓ ┏━┛
//    ┃ ┫ ┫   ┃ ┫ ┫
public class MainActivity extends BaseFragmentActivity implements MaterialTabListener {
    MaterialTabHost tabHost;
    ViewPager pager;
    ViewPagerAdapter adapter;

    AccountFragment accountFragment;
    CircleFragment circleFragment;
    MainFragment mainFragment;
    MoreFragment moreFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        AVOSCloud.initialize(this, "XlXBD07fYYUKR9TWj6Mj8WCQ-gzGzoHsz", "E1upqpyavuU6sS1ezX5VTzBs");
        accountFragment = new AccountFragment();
        circleFragment = new CircleFragment();
        mainFragment = new MainFragment();
        moreFragment = new MoreFragment();

        // init view pager
        adapter = new ViewPagerAdapter(getSupportFragmentManager());

        initViews();
        initEvents();


    }

    protected void initViews() {
        tabHost = (MaterialTabHost) this.findViewById(R.id.main_materialTabHost);
        pager = (ViewPager) this.findViewById(R.id.main_pager);

    }

    protected void initEvents() {
        pager.setAdapter(adapter);
        pager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                // when user do a swipe the selected tab change
                tabHost.setSelectedNavigationItem(position);

            }
        });
        // insert all tabs from pagerAdapter data
        for (int i = 0; i < adapter.getCount(); i++) {
            tabHost.addTab(
                    tabHost.newTab()
                            .setText(adapter.getPageTitle(i))
                            .setTabListener(this)
            );

        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        /*然后在碎片中调用重写的onActivityResult方法*/
        accountFragment.onActivityResult(requestCode, resultCode, data);
    }


    @Override
    public void onTabSelected(MaterialTab tab) {
        pager.setCurrentItem(tab.getPosition());
    }

    @Override
    public void onTabReselected(MaterialTab tab) {

    }

    @Override
    public void onTabUnselected(MaterialTab tab) {

    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    private class ViewPagerAdapter extends FragmentStatePagerAdapter {

        public ViewPagerAdapter(FragmentManager fm) {
            super(fm);


        }

        public Fragment getItem(int num) {
            Fragment returnFragment = null;
            switch (num) {
                case 0:
                    returnFragment = mainFragment;
                    break;
                case 1:
                    returnFragment = accountFragment;
                    break;
                case 2:
                    returnFragment = circleFragment;
                    break;
                case 3:
                    returnFragment = moreFragment;
                    break;
            }
            return returnFragment;
        }

        @Override
        public int getCount() {
            return 4;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            String title = "";
            switch (position) {
                case 0:
                    title = getResources().getString(R.string.mainfragment);
                    break;
                case 1:
                    title = getResources().getString(R.string.account);
                    break;
                case 2:
                    title = getResources().getString(R.string.circle);
                    break;
                case 3:
                    title = getResources().getString(R.string.more);
                    break;
            }
            return title;
        }

    }
}
